<template>
  <div class="agentAmount">
    <div class="topBar">
      <div class="searchBar">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="68px">
          <el-form-item label="用户昵称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入用户昵称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="operateBar">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:money:add']"
        >新增</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:money:remove']"
        >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:money:export']"
        >导出</el-button>
      </div>
    </div>
    <el-table
      stripe
      height="100%"
      style="width: 100%"
      :data="moneyList"
      v-loading="loading"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代理师" prop="nickName" />
      <el-table-column label="发明专利" prop="materialsNew" />
      <el-table-column label="优审发明" prop="materialsNewNo" />
      <el-table-column label="预审发明" prop="invent" />
      <el-table-column label="新型专利" prop="inventNo" />
      <el-table-column label="其它案件" prop="inventions" />
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:money:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:money:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代理人金额对话框  @select="handleSelect" -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      :before-close="cancel">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
        <el-form-item label="代理师" prop="userId">
          <el-select
            style="width: 100%"
            filterable
            v-model="form.userId"
            placeholder="请选择">
            <el-option
              v-for="item in salesmanList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId">
              <span style="float: left">{{ item.nickName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phonenumber }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发明专利" prop="materialsNew">
          <el-input v-model="form.materialsNew" placeholder="请输入发明专利" />
        </el-form-item>
        <el-form-item label="优审发明" prop="materialsNewNo">
          <el-input v-model="form.materialsNewNo" placeholder="请输入优审发明" />
        </el-form-item>
        <el-form-item label="预审发明" prop="invent" >
          <el-input v-model="form.invent" placeholder="请输入预审发明" />
        </el-form-item>
        <el-form-item label="新型专利" prop="inventNo" >
          <el-input v-model="form.inventNo" placeholder="请输入新型专利" />
        </el-form-item>
        <el-form-item label="其它案件" prop="inventions" >
          <el-input v-model="form.inventions" placeholder="请输入其它案件" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMoney, getMoney, delMoney, addMoney, updateMoney,listBusiness } from "@/api/agentAmount";
export default {
  name: "Money",
  data() {
    return {
      queryParams: {
        nickName: null,
        pageNum: 1,
        pageSize: 10,
      },

      moneyList: [],
      total: 0,
      loading: true,
      ids: [],
      multiple: true,

      title: "",
      open: false,
      form: {
        id: null,
        userId: null,
        materialsNew: null,
        materialsNewNo: null,
        invent: null,
        inventNo: null,
        inventions: null
      },
      rules: {
        userId: [
          { required: true, message: "请选择代理师", trigger: "blur" }
        ],
      },
      salesmanList: []
    };
  },
  created() {
    this.getSalesmanList()
    this.getList();
  },
  methods: {
    getSalesmanList() {
      const params = {
        nickName: "",
        phonenumber: "",
      }
      listBusiness(params).then(response => {
        const { code, rows } = response;
        if(code === 200) this.salesmanList = rows;
      })
    },
    /** 查询代理人金额列表 */
    getList() {
      this.loading = true;
      listMoney(this.queryParams).then(response => {
        this.moneyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除代理人金额编号为"' + ids + '"的数据项？').then(function() {
        return delMoney(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/money/export', {
        ...this.queryParams
      }, `money_${new Date().getTime()}.xlsx`)
    },
    // 取消按钮
    cancel() {
      this.$refs['form'].resetFields();
      this.open = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加代理人金额";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getMoney(id).then(response => {
        this.form = { ...response.data  };
        this.open = true;
        this.title = "修改代理人金额";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(this.form);
        if (valid) {
          if (this.form.id != null) {
            updateMoney(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.cancel();
              this.getList();
            });
          } else {
            addMoney(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.cancel();
              this.getList();
            });
          }
        }
      });
    },
}};
</script>
<style lang="scss" scoped>
div.agentAmount {
  div.topBar {
    height: 36px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div.searchBar {
      .el-form-item {
        margin-bottom: 0;
      }
    }
  }
}
</style>
